package com.js.springbootredissignin.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.js.springbootredissignin.entity.Student;
import com.js.springbootredissignin.entity.StudentPojo;
import com.js.springbootredissignin.mapper.StudentMapper;
import com.js.springbootredissignin.service.StudentService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private RedisTemplate redisTemplate;



    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询全部签到的人数，签到存储的时间为一个小时
     * @return
     */
    @Override
    public Set<Student> selectAllIT() {
        Set<Student> list = redisTemplate.opsForZSet().range("studentIT3_key",0,-1);
        redisTemplate.expire("studentIT3_key",60, TimeUnit.MINUTES);
        Integer number = Math.toIntExact(redisTemplate.opsForZSet().size("studentIT3_key"));
        System.out.println("签到人数："+number);
        return list;
    }



    @Override
    public int SignIn(String name) {
        Student student = studentMapper.selectByName(name);
        if (student!=null){
            student.setStatus(1);
            int result = studentMapper.updateById(student);
            System.out.println("存储："+student);
            if (result==1){
                RedisAtomicLong entityIdCounter = new RedisAtomicLong("number", redisTemplate.getConnectionFactory());
                Long increment = entityIdCounter.getAndIncrement();
                redisTemplate.opsForZSet().add("studentIT3_key",student,increment);
                return 1;
            }
        }
        return 0;
    }

    /**
     * 查询未签到人员
     * @return
     */
    @Override
    public List<Student> noSignIn() {
        Integer status=0;
        List<Student> list = studentMapper.selectNoSignIn(status);
        return list;
    }

    @Override
    public int selectCountIT() {
        Integer number = Math.toIntExact(redisTemplate.opsForZSet().size("studentIT3_key"));
        return number;
    }


    @Override
    public int add(Student student) {
        int result = studentMapper.insert(student);
        return result;
    }


    @Override
    public int updateSignIn() {
        int result =0;
        List<Student> list = studentMapper.selectList(null);
        for (Student student1:list){
            student1.setStatus(0);
             result = studentMapper.updateById(student1);
        }
        return result;
    }

    @Override
    public int sum() {
        int result = studentMapper.selectCount(null);
        return result;
    }


    @Override
    public StudentPojo selectNoCount() {
        Integer status = 0;
        StudentPojo result = studentMapper.selectNoCount(status);
        return result;
    }
}
